#!/bin/bash

#jenkins项目分支名
#Project=xxxxxx
Env=fortest
#操作类型-deploy部署，rollback 回滚
operType=deploy
#ssh 插件默认部署包路径
sshPluginDefaultDeployPath=/usr/mpsp/jenkinsTemp/$Project
#要部署的项目HOME 路径
deployProjectHome=/usr/mpsp/temp/pl-app-busi_fortest
deployProjectName=pl-app-busi
packageProjectName=${Project}_${Env}.tar.gz
uploadProject=$deployProjectHome/upload
backProject=$deployProjectHome/back


#区分web项目还是jar项目
#1.确认项目基本信息（自动创建project,back,upload,目录）
#2.备份项目(排除log目录江项目完整备份到back目录)
#3.部署还是回滚（回滚只能回滚到上一次备份的）


#deploy
#1.backupProject 2.deployProject 3.runProject
#rollback
#1.rollbackProject 2.runProject

export LANG=zh_CN.UTF-8
source /etc/profile

#back function
backupProject(){
	#backOperation $1:deployProjectHome $2:deployProjectName $3:backProject
	echo "project backup start ..."
	cd $deployProjectHome
	datetime=`date +"%Y%m%d%H%M%S"`
	backProjectFileName=${deployProjectName}.${datetime}.tar.gz
	tar -zcv $backProjectFileName --exclude=log $deployProjectName
	mv $backProjectFileName $backProject
	echo "project backup finished, back file name : $backProjectFileName"
}

#deployFunction
deployProject(){
	cp -R $sshPluginDefaultDeployPath/$packageProjectName $uploadProject/
	cd $uploadProject/
	tar -zxvf  $packageProjectName  -C  $deployProjectHome/
}
#runProjectFunction
runProject(){
	echo `java -version`
	cd $deployProjectHome/$deployProjectName/bin
	chmod +x *.sh
	chmod -R 777 *
	. para
	. classpath
	
	pidold=$(ps -ef| grep -v grep |grep ${app_name} |awk '{printf $2}')
	#stop app 
	if [ -n "${pidold}" ]
	then
		ps -ef|grep ${app_name}|grep -v grep|grep -v tail|awk 'BEGIN{printf "kill "}{printf "%s ", $2}'|bash
        echo "Service has been closed"
	else
		echo ""
	fi
	#start app
	nohup java -Dflag=${app_name} -cp ${APPCLASSPATH} ${app_main_class} > /dev/null 2>&1 &
	sleep 1s
	appPid=$(ps -ef| grep -v grep |grep ${app_name} |awk '{printf $2}')
	sleep 3s
	if [ -n "${appPid}" ]
	then
	    echo "===============  Service has started , PROCESS ID : ${appPid} ===============" 
		host=$(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|grep "10.10"|awk '{print $2}'|tr -d "addr:")
		echo "===============  http://${host}:${port} ===============" 
		
		logFile=$deployProjectHome/$deployProjectName/log/spring.log
		echo $logFile
		if [ -r $logFile ];then
			export LANG=zh_CN.GBK
			tail -25 $logFile
		fi
	else
		echo "===============  Service NOT FOUND   ===============" 
	fi
	
}
#rollbackProjectFuncion
rollbackProject(){
	#$1:backProject $2:deployProjectName
	#最后一次备份的文件名：
	lastBackProjectName=`ls $backProject -t | grep $deployProjectName  | head -n 1`
	echo "备份文件:$lastBackProjectName"
	if test -z $lastBackProjectName
	then 
		echo "not found backup project , exit  ! (无备份文件，退出)"
		exit 0;
	else
	    rollbackProjectName=$deployProjectHome/${deployProjectName}_rollback
		if [ -d $rollbackProjectName ];then
			rm -rf $rollbackProjectName
		fi
	    mv $deployProjectHome/$deployProjectName $deployProjectHome/${deployProjectName}_rollback
		tar -zxvf  $lastBackProjectName  -C  $deployProjectHome/
	fi
}


#判断项目路径是否存在
if [ ! -d $deployProjectHome ];then
  mkdir $deployProjectHome
fi
#判断上传路径是否存在不存在则创建
if [ ! -d $uploadProject ];then
  mkdir $uploadProject
fi
#判断备份路径是否存在不存在则创建
if [ ! -d $backProject ];then
  mkdir $backProject
fi

if [ $operType == "deploy" ] 
then
	#1.back project
	backupProject
	#2.deployProject
	deployProject
	#3.run Project
	runProject
elif [ $operType == "rollback"] 
then 
	#1.rollback
	rollbackProject
	#2.runProject
	runProject
else 
	echo "不支持操作"
fi


echo "deploy finished ... "